<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once('app/libraries/excel/PHPExcel.php');
$chart = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
$page_title = "Thống kê theo domain từ ngày ". date('d/m/Y', strtotime($fromdate))." đến ngày ". date('d/m/Y', strtotime($todate));
		// Create new PHPExcel object
    	$objPHPExcel = new PHPExcel();
    	// Set properties
    	$objPHPExcel->getProperties()->setCreator("AdMarket")
    								 ->setLastModifiedBy("AdMarket")
    								 ->setTitle("Office 2007 XLSX Document")
    								 ->setSubject("Office 2007 XLSX Document")
    								 ->setDescription($page_title)
    								 ->setCategory("");
	
	   $styleArray = array(
			'font' => array(
				'bold' => true,
			),
			'alignment' => array(
				'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
			),
			'borders' => array(
				'top' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN,
				),
			)
		);
	    				
    	$objPHPExcel->getActiveSheet()->mergeCells($chart[0].'1:'.$chart[count($label) - 1].'1');
    	$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $page_title);
    	$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);
		
	   // Add some data
    	foreach ($label as $i	=>	$title)
    	{
	    	$objPHPExcel->setActiveSheetIndex(0)
	    				->setCellValue($chart[$i].'2', $title);
	    	$objPHPExcel->setActiveSheetIndex(0)
	    				->getColumnDimension($chart[$i])->setAutoSize(true);
	    	$objPHPExcel->getActiveSheet()->getStyle($chart[$i].'2')->applyFromArray($styleArray);
    	}
        $index = 3;
        foreach($items as $i	=>	$item)
        {
        	foreach($label as $j	=>	$title)
        	{
	        	$objPHPExcel->setActiveSheetIndex(0)
	    					->setCellValue($chart[$j].$index, $item->$title);
	        }
            $index++;
        }

        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="AdMarket_transaction_report.xls"');
        header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');   
    	$objWriter->save('php://output');
    	exit;